
import LogicConfigTs, {default as LogicConfigTs} from "../Utils/LogicConfigTs";
import LanguageMngTs from "./LanguageMngTs";

let GameWS = require("GameWS")
let HubConnection = require("HubConnection")
export default class RoomController{

    public static async startFindRoom (roomObj) {
        let self = this
        cc.log("尝试寻找房间：",roomObj)
        await cc.gg.toastMgr.showNetWaitting()
        this.openGameScene(roomObj.id,async function () {
            await cc.gg.toastMgr.showNetWaitting()
            await self.waitTime(1000)
            //收拾一下桌面，初始化一下
            let wsEnable = await self.checkWS()
            if(!wsEnable){
                cc.log("目前socket未连接，等待连接上")

                cc.find("Canvas").emit("msgwatcher_add",function () {
                    if(GameWS.getState() === HubConnection.HubConnectionState.Connected){
                        cc.log("socket连接上了，开始寻找room")
                        self._doFindRoom(roomObj)
                        return true
                    }
                    return false
                }.bind(self))
            }else {
                cc.log("socket状态正常，开始寻找room")
                self._doFindRoom(roomObj)
            }
        })
    }

    //million
    public static async waitTime(time){
        return new Promise((resole)=>{
            setTimeout(function () {
                resole()
            }.bind(this),time)
        })
    }

    public static openGameScene(mode,cb){
        let roomMode = cc.gg.enumConfig.RoomMode
        let modeBase = mode & 0x0f0
        if(modeBase === roomMode.BASE_VALUE_2){
            cc.log("进入场景2")
            cc.director.loadScene(LogicConfigTs.SCENENAME_GAME_2,cb)
        }else if(modeBase === roomMode.BASE_VALUE_6){
            cc.log("进入场景6")
            cc.director.loadScene(LogicConfigTs.SCENENAME_GAME_6,cb)
        }
    }

    public static async _doFindRoom(roomObj){
        let type = roomObj.type
        let mode = roomObj.id
        let joker = true
        LogicConfigTs.LAST_ENTERED_ROOM = roomObj.id;
        let code = await GameWS.findRoom(type, mode, joker)
        cc.gg.toastMgr.removeNetWaitting()
        if(!code){//success
            cc.gg.gameDataUtil.roomInfo = roomObj
        }else if(code == 2){//already has a room
            this._findRoomFailed(code)
        }
        else {
            cc.gg.toastMgr.removeNetWaitting()
            this._findRoomFailed(code)
        }
    }

    public static async checkWS () {
        let wsStatus = GameWS.getState()
        cc.log(" 检查ws状态：",wsStatus)
        if(wsStatus !== HubConnection.HubConnectionState.Connected){
            return false
        }

        return true
    }

    public static async _findRoomFailed (code) {
        if(code == 2){
            cc.logfl("已经在游戏中了")
            cc.gg.baseUtil.setLandscape(1)
            cc.director.loadScene("LobbyBlue")
        }else if(code == 5){
            let title = LanguageMngTs.getSentence(3)
            let btnStr = LanguageMngTs.getSentence(179)
            let buyStr = LanguageMngTs.getSentence(182)
            let contentStr = LanguageMngTs.getSentence(180)
            let cb = function () {
                cc.gg.toastMgr.removeDialog()
            }.bind(this)
            // cc.gg.toastMgr.showDialogOneButton(title,btnStr,contentStr,cb)

            cc.gg.toastMgr.showDialog(title,buyStr,btnStr,contentStr,
                function () {
                    cc.gg.tdUtil.doSendTdEvent("show_wallet","MoneyNotEnough")
                    cc.gg.toastMgr.removeDialog()
                    let sceneName = cc.gg.logicUtil.getCurrentSceneName()
                    if(sceneName === LogicConfigTs.SCENENAME_LOBBY){
                        cc.find("Canvas/UIManager").getComponent("LobbyUIMgr").tryToShowWallet()
                    }else {
                        cc.gg.gameDataUtil.goToLobby(true);
                    }
                }.bind(this),
                cb
            )
        }
    }
}
